home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 302_01.zip / MM.C < prev    next >
Text File  |  1993-04-09  |  2KB  |  65 lines

  1. /* Matrix multiplication
  2.  
  3.    Copyright (c) 1988 by Gus O'Donnell
  4.  
  5.    Revision history:
  6.  
  7.    Version 1.00         February 29, 1988       As released.
  8.  
  9.    Version 1.01         March 20, 1988          Created libraries for all
  10.                                                 memory models
  11.  
  12. */
  13. #include <3d.h>
  14. #include <float.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17.  
  18. void    assign_mat(MATRIX mat1, MATRIX mat2)
  19.  
  20. /* Copy mat2 to mat1. */
  21.  
  22. {
  23.     int rcount,ccount;
  24.  
  25.     for (rcount = 0; rcount < RMAX; rcount++)
  26.         for (ccount = 0; ccount < CMAX; ccount++)
  27.             mat1 [rcount] [ccount] = mat2 [rcount] [ccount];
  28. }
  29.  
  30. void    mat_mul (MATRIX mat1, MATRIX mat2, MATRIX prod)
  31.  
  32. /* Multiply two matrices.  An element of the product matrix prod [i] [j]
  33. is the dot product of row i of mat1 and column j of mat2, that is:
  34.  
  35.            prod [i] [j] = mat1 [i] [0] * mat2 [0] [j]
  36.                         + mat1 [i] [1] * mat2 [1] [j]
  37.                         + mat1 [i] [2] * mat2 [2] [j]
  38.                         + mat1 [i] [3] * mat2 [3] [j]
  39.  
  40. The matrices must be conformal, i.e., the number of columns of mat1
  41. must equal the number of rows of mat2.  Since all transformation
  42. matrices are 4x4, they are always conformal.  Notice that the operation
  43. is not commutative--in general, mat1 * mat2 != mat2 * mat1.  This has
  44. important implications for 3D transforms, for example, a rotation about
  45. [0,0,0], followed by a translation is not the same as a translation
  46. followed by a rotation.  */
  47.  
  48. {
  49.     int i,j,p;  /* i indexes the row of the product matrix,
  50.                    j indexes the column,
  51.                    p indexes the product term while
  52.                    calculating the dot product. */
  53.     MATRIX result;
  54.  
  55.     for (i = 0; i < RMAX; i++)
  56.         for (j = 0; j < CMAX; j++)
  57.         {
  58.             result [i] [j] = 0.0;
  59.             for (p = 0; p < RMAX; p++)
  60.                 result [i] [j] = result [i] [j]
  61.                                + mat1 [i] [p] * mat2 [p] [j];
  62.         }
  63.     assign_mat (prod,result);
  64. }
  65.